package com.example.lotterysystem.service.impl;

import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageParam;
import com.example.lotterysystem.dao.dataobject.PrizeDo;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.PrizeService;
import com.example.lotterysystem.service.dto.PageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@Service
public class PrizeServiceImpl implements PrizeService {

    @Autowired
    private PictureServiceImpl pictureService;
    @Autowired
    private PrizeMapper prizeMapper;
    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        String fileName = pictureService.savePicture(picFile);
        PrizeDo prizeDo = new PrizeDo();
        prizeDo.setName(param.getPrizeName());
        prizeDo.setDescription(param.getDescription());
        prizeDo.setImageUrl(fileName);
        prizeDo.setPrice(param.getPrice());
        prizeMapper.insert(prizeDo);
        return prizeDo.getId();
    }

    @Override
    public PageListDTO<PrizeDTO> findPrizeList(PageParam param) {
        int total = prizeMapper.count();
        // 查询当前页列表
        List<PrizeDTO> prizeDTOList = new ArrayList<>();
        List<PrizeDo> prizeDoList = prizeMapper.selectPrizeList(param.offset(), param.getPageSize());
        for(PrizeDo prizeDo : prizeDoList){
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDo.getId());
            prizeDTO.setName(prizeDo.getName());
            prizeDTO.setDescription(prizeDo.getDescription());
            prizeDTO.setImageUrl(prizeDo.getImageUrl());
            prizeDTO.setPrice(prizeDo.getPrice());
            prizeDTOList.add(prizeDTO);
        }
        return new PageListDTO<>(total , prizeDTOList);
    }
}



